package com.lhd.qd.module.system.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.common.base.BaseServiceImpl;
import com.lhd.qd.module.system.hospital.dao.HospitalMapper;
import com.lhd.qd.module.system.hospital.model.converter.AbstractHospitalConverter;
import com.lhd.qd.module.system.hospital.model.dataobj.HospitalDO;
import com.lhd.qd.module.system.hospital.model.dto.HospitalSaveDTO;
import com.lhd.qd.module.system.hospital.model.vo.HospitalDetailVO;
import com.lhd.qd.module.system.hospital.model.vo.HospitalListVO;
import com.lhd.qd.module.system.hospital.model.vo.HospitalTreeVO;
import com.lhd.qd.module.system.hospital.service.HospitalService;
import com.lhd.qd.module.system.resource.model.converter.AbstractResourceConverter;
import com.lhd.qd.module.system.resource.model.dataobj.ResourceDO;
import com.lhd.qd.module.system.resource.model.vo.MenuTreeVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HospitalServiceImpl extends BaseServiceImpl<HospitalMapper, HospitalDO> implements HospitalService {

    @Override
    public IPage<HospitalListVO> pageHospital(BasePageQuery query) {

        LambdaQueryWrapper<HospitalDO> queryWrapper = Wrappers.<HospitalDO>lambdaQuery();

        IPage<HospitalDO> doPage = page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);

        IPage<HospitalListVO> voPage = AbstractHospitalConverter.INSTANCE.doPage2ListVOPage(doPage);

        return voPage;
    }

    @Override
    public List<HospitalTreeVO> getHospitalTree() {

        LambdaQueryWrapper<HospitalDO> queryWrapper = Wrappers.<HospitalDO>lambdaQuery()
                .orderByAsc(HospitalDO::getParentId);

        List<HospitalDO> doList = list(queryWrapper);

        return buildTree(0L, doList);
    }

    @Override
    public HospitalDetailVO getHospitalById(Long id) {

        HospitalDetailVO vo = AbstractHospitalConverter.INSTANCE.do2DetailVO(getById(id));

        return vo;
    }

    @Override
    public boolean saveHospital(HospitalSaveDTO saveDTO) {

        HospitalDO dataObj = AbstractHospitalConverter.INSTANCE.saveDTO2DO(saveDTO);

        return save(dataObj);
    }

    @Override
    public boolean updateHospitalById(Long id, HospitalSaveDTO saveDTO) {

        HospitalDO dataObj = AbstractHospitalConverter.INSTANCE.saveDTO2DO(saveDTO);

        dataObj.setId(id);

        return updateById(dataObj);
    }

    @Override
    public boolean removeHospitalById(Long id) {

        return removeById(id);
    }

    private static List<HospitalTreeVO> buildTree(Long parentId, List<HospitalDO> doList) {

        List<HospitalTreeVO> treeList =  new ArrayList<>();
        for (HospitalDO dataObj : doList) {

            if (parentId.equals(dataObj.getParentId())) {

                HospitalTreeVO vo = AbstractHospitalConverter.INSTANCE.do2TreeVO(dataObj);
                vo.setChildren(buildTree(dataObj.getId(), doList));

                treeList.add(vo);
            }
        }

        return treeList;
    }
}
